package com.image.demo.common.init;

import java.io.File;
import java.io.FileOutputStream;


import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.image.demo.common.utils.ImgTools;

/**
 * 初始化时处理图片
 * @author zlj
 *
 */
@Component
public class MyApplicationRunner implements ApplicationRunner{

	@Override
	public void run(ApplicationArguments args) throws Exception {
		dealImg();
		System.out.println("-------图片处理完成--------");
	}
	
	
	
	/**
	 * 压缩图片
	 * @param request
	 */
	private void dealImg() {
		
		try {
			String sysPath = System.getProperty("user.dir");
			String rootPath = sysPath + "\\src\\main\\webapp\\";
			String[] types =new String[] {"fdmc","ty","tyfspt","tymhgd","tywl","tyxm"};//所有图片类型
			for (String type : types) {
				String path = rootPath+"static/oldImage/"+type;
				String outPath = rootPath+"static/images/"+type;
				File folder = new File(path);
				File[] listFiles = folder.listFiles();
				if(null != listFiles && listFiles.length > 0)
					for (File file : listFiles) {
						File newFile = new File(outPath+File.separator+file.getName());
						if(!newFile.exists()) {
							FileOutputStream fos = new FileOutputStream(outPath+File.separator+file.getName());
							ImgTools.thumbnail_w_h(file, 400, 0, fos);
						}
					}
			}
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
